package cn.js.ccit.flowersmall.order.controller;

import cn.js.ccit.common.dubbo.GoldCoinService;
import cn.js.ccit.flowersmall.order.entity.GiftOrderEntity;
import cn.js.ccit.flowersmall.order.entity.OrderEntity;
import cn.js.ccit.flowersmall.order.service.GiftGoldCoinService;
import cn.js.ccit.flowersmall.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.List;

@RestController
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    GiftGoldCoinService giftGoldCoinService;

    @GetMapping("/orders")
    public List<OrderEntity> orders(String username){
        // 获取当前登录用户
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof UserDetails){
            username = ((UserDetails)principal).getUsername();
        }else {
            username = principal.toString();
        }
        System.out.println("user:" + username);

        return orderService.findOrders(username);
    }

    @GetMapping("/giftOrders")
    public List<GiftOrderEntity> giftOrders(String username){
        // 获取当前登录用户
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof UserDetails){
            username = ((UserDetails)principal).getUsername();
        }else {
            username = principal.toString();
        }
        System.out.println("user:" + username);

        return orderService.findGiftOrders(username);
    }
   @DubboReference(check = false)
    GoldCoinService goldCoinService;

    @GlobalTransactional
    @PostMapping("/order")
    public String order(HttpServletRequest request,
                        @RequestBody OrderEntity order) {
        String username = "";
        Object principal = SecurityContextHolder.
                getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            username = ((UserDetails) principal).getUsername();
        } else {
            username = principal.toString();
        }
        order.setUserName(username);
        order.setOrderNo(Long.toString(System.currentTimeMillis()));
        order.setCreateTime(new Date(System.currentTimeMillis()));
        //System.out.println(order);
        //创建订单
        orderService.addOrder(order);

        // 调用 goldCoin service
        int goldCoinNums = (new Double(order.getAmount() / 10)).intValue();
        goldCoinService.updateGoldCoin(username, goldCoinNums, order.getOrderNo());

        return "ok";
    }
    //@GlobalTransactional
    @PostMapping("/gift/order")
    public String giftOrder(HttpServletRequest request, @RequestBody GiftOrderEntity order) {
        String username = "";
        Object principal = SecurityContextHolder.getContext().
                               getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            username = ((UserDetails) principal).getUsername();
        } else {
            username = principal.toString();
        }
        order.setUserName(username);
        order.setOrderNo(Long.toString(System.currentTimeMillis()));
        order.setCreateTime(new Date(System.currentTimeMillis()));
        System.out.println(order);
        orderService.addGiftOrder(order);

        // 调用 gift goldCoin service
       // int goldCoinNums = (new Double(order.getAmount() / 100)).intValue();
       // goldCoinService.updateGoldCoin(username, goldCoinNums, order.getOrderNo());
        giftGoldCoinService.updateGoldCoin(username,-order.getGoldCoin(),order.getOrderNo());
        return "ok";
    }
}
